package es.goldbittle.android;

import java.io.IOException;
import java.net.SocketException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import android.app.Application;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
//import quinielaGold.beans.DatosForo;
import quinielaGold.beans.DatosPostForo;
import quinielaGold.beans.DatosComentarioForo;
import quinielaGold.beans.DatosGenerales;
import quinielaGold.beans.DatosPostForoSimple;
import quinielaGold.beans.Persona;
import quinielaGold.beans.Usuario;
import quinielaGold.beans.Quiniela;
import quinielaGold.beans.DatosRanking;
import quinielaGold.beans.Apuesta;
import quinielaGold.excepciones.ExcepcionConexion;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.Marshal;
import org.ksoap2.serialization.MarshalFloat;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

public class QuiniGoldApp extends Application{
	
 	public String ORIGEN_SELECCION_NAME = "ORIGEN";
 	public int 	ORIGEN_SELECCION_RANKING_EFECTIVIDAD	= 1;
 	public int 	ORIGEN_SELECCION_RANKING_PUNTOS			= 2;
 	public int 	ORIGEN_SELECCION_QUINIELA_FINAL = 3;
 	public boolean bVenimosDeSeleccionarTemporadaJornada = false;
 	public boolean bSeleccionadaTemporadaJornada = false;
 	private int qgTemporadaSeleccionada = 0;
 	private int qgJornadaSeleccionada = 0;
 	

	//Variables Globales Aplicacion
	//-------------------------------------------------------------------------------
	private int Jornada = 0; 
	private Usuario qgUsuario = null;
	private DatosGenerales qgDatosGenerales = null;
	private Persona qgPersona = null;
	private Quiniela[] qgQuiniela = null;
	
	private DatosRanking[] qgRankingPuntosGlobal;
	private DatosRanking[] qgRankingEfectividadGlobal;
	
	//private DatosForo[] qgForo;
	private DatosPostForo[] qgPostsForo;
	private int qgPaginacionPostForo;
	private int qgCantidadPostForo	= 10;
	
	private DatosComentarioForo[] qgComentariosForo;
	private int qgPaginacionComentariosForo;
	private int qgCantidadComentariosForo = 20;
	
	private DatosPostForo qgPostForoActual;

	private Apuesta[] qgApuestas;
	
	private int[] qgTemporadas;
	private int[] qgJornadas;
	
	private int qgOrigen;
	
 	public int 	ORIGEN_ORIGEN_FORO_UPDATE		= 1;
 	public int 	ORIGEN_ORIGEN_FORO_ADELANTE		= 2;
 	public int 	ORIGEN_ORIGEN_FORO_ATRAS 		= 3;
	private int qgOrigenForo = 0;
	
	private int cont_selectTempJor=0;
	
	
	//Metodos Util Comunes
	//-------------------------------------------------------------------------------
	public String formatearFecha (String fecha)
	{
		String fechaFormateada = "";
		
		try{
            DateFormat df =new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
            Date result = df.parse(fecha);
            
            SimpleDateFormat formato = new SimpleDateFormat("MMM d H:mm");
            fechaFormateada = formato.format(result);
		}
		catch (Exception e)
		{
			return "";
		}
		return fechaFormateada;
		
	}
	public String getFechaActualFormateada()
	{
		
        DateFormat df =new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        return df.format(new Date());

	}
	//Metodos Acceso Variables Globales
	//-------------------------------------------------------------------------------
	public int getContSelectTempJor() {
		return cont_selectTempJor;
	}
	public void setContSelectTempJor(int cont_selectTempJor) {
		this.cont_selectTempJor = cont_selectTempJor;
	}
	public void incrementContSelectTempJor() {
		this.cont_selectTempJor++;
	}
	public int getOrigenForo() {
		return qgOrigenForo;
	}
	public void setOrigenForo(int qgOrigenForo) {
		this.qgOrigenForo = qgOrigenForo;
	}
	public DatosPostForo getPostForoActual() {
		return qgPostForoActual;
	}
	public void setPostForoActual(DatosPostForo qgPostForoActual) {
		this.qgPostForoActual = qgPostForoActual;
	}
	public int getCantidadPostForo() {
		return qgCantidadPostForo;
	}
	public void setCantidadPostForo(int qgCantidadPostForo) {
		this.qgCantidadPostForo = qgCantidadPostForo;
	}
	public int getCantidadComentariosForo() {
		return qgCantidadComentariosForo;
	}
	public void setCantidadComentariosForo(int qgCantidadComentariosForo) {
		this.qgCantidadComentariosForo = qgCantidadComentariosForo;
	}
	public DatosPostForo[] getQgPostForo() {
		return qgPostsForo;
	}
	public void setQgForo(DatosPostForo[] qgPostsForo) {
		this.qgPostsForo = qgPostsForo;
	}
	public void resetForo() {
		this.qgPostsForo = null;
	}
	public int getPaginacionPostForo() {
		return qgPaginacionPostForo;
	}
	public void setPaginacionPostForo(int paginacionPostForo) {
		if (paginacionPostForo < 0)
			this.qgPaginacionPostForo = 0;
		else
			this.qgPaginacionPostForo = paginacionPostForo;
	}
	public DatosComentarioForo[] getQgComentariosForo() {
		return qgComentariosForo;
	}
	public void setQgForo(DatosComentarioForo[] qgComentariosForo) {
		this.qgComentariosForo = qgComentariosForo;
	}	
	public int getPaginacionComentariosForo() {
		return qgPaginacionComentariosForo;
	}
	public void setPaginacionComentariosForo(int paginacionComentariosForo) {
		this.qgPaginacionComentariosForo = paginacionComentariosForo;
	}
	public int[] getTemporadas(){
		return qgTemporadas;
	}
	public void setTemporadas(int[] tem){
		qgTemporadas = tem;
	}
	
	public int[] getJornadas(){
		return qgJornadas;
	}
	public void setJornadas(int[] jor){
		qgJornadas = jor;
	}

	public int getTemporadaSeleccionada(){
		return qgTemporadaSeleccionada;
	}
	public void setTemporadaSeleccionada(int or){
		qgTemporadaSeleccionada = or;
	}
	public int getJornadaSeleccionada(){
		return qgJornadaSeleccionada;
	}
	public void setJornadaSeleccionada(int or){
		qgJornadaSeleccionada = or;
	}
	public int getOrigen(){
		return qgOrigen;
	}
	public void setOrigen(int or){
		qgOrigen = or;
	}
	public Apuesta[] getApuestasUsuario(){
		return qgApuestas;
	}
	public void setApuestasUsuario(Apuesta[] apu){
		qgApuestas = apu;
	}
	
	public DatosRanking[] getRankingPuntosGlobal(){
		return qgRankingPuntosGlobal;
	}
	public void setRankingPuntosGlobal(DatosRanking[] dr){
		qgRankingPuntosGlobal = dr;
	}

	public DatosRanking[] getRankingEfectividadGlobal(){
		return qgRankingEfectividadGlobal;
	}
	public void setRankingEfectividadGlobal(DatosRanking[] dr){
		qgRankingEfectividadGlobal = dr;
	}

	public Usuario getUsuario(){
	    return qgUsuario;
	  }
	public void setUsuario(Usuario u){
		qgUsuario = u;
	  }
	
	public DatosGenerales getDatosGenerales(){
	    return qgDatosGenerales;
	  }
	public void setDatosGenerales(DatosGenerales dg){
		qgDatosGenerales = dg;
	  }
	
	public Persona getPersona(){
	    return qgPersona;
	  }
	public void setPersona(Persona p){
		qgPersona = p;
	  }
	
	public Quiniela[] getQuiniela(){
	    return qgQuiniela;
	  }
	public void setQuiniela(Quiniela[] lq){
		qgQuiniela = lq;
	  }

	private void inicializa()
	{
		//qgUsuario = null;
		qgDatosGenerales = null;
		qgPersona = null;
		qgQuiniela = null;
		
		qgRankingPuntosGlobal = null;
		qgRankingEfectividadGlobal = null;
		qgPostsForo = null;
		qgPaginacionPostForo = 0;
		qgComentariosForo = null;
		qgPaginacionComentariosForo = 0;
		
		qgApuestas = null;		
	}
	
	//Metodos Acceso Web Services GOLDBITTLE.es
	//-------------------------------------------------------------------------------
 	//private static final String NAMESPACE = "http://goldbittle.es/QuiniGoldWS/services";
	//private static final String URL = "http://goldbittle.es/QuiniGoldWS/services/QuiniGoldFnImpl?wsdl";
	//private static final String SOAP_ACTION = "QuiniGoldFnImpl";

	//OPENSHIFT
 	private static final String NAMESPACE = "http://quinigold-racsor.rhcloud.com/services";
 	private static final String URL = "http://quinigold-racsor.rhcloud.com/services/QuiniGoldFnImpl?wsdl";
	private static final String SOAP_ACTION = "QuiniGoldFnImpl";
	
	//LOCALHOST
// 	private static final String NAMESPACE = "http://10.0.2.2:8080/quinigold/services";
// 	private static final String URL = "http://10.0.2.2:8080/quinigold/services/QuiniGoldFnImpl?wsdl";
//	private static final String SOAP_ACTION = "QuiniGoldFnImpl";
	
	

	private static final String METHOD_NAME_LOGIN = "login";
	private static final String METHOD_NAME_OBTENERPERSONA = "obtenerPersona";
	private static final String METHOD_NAME_OBTENERQUINIELA = "obtenerQuiniela";
	private static final String METHOD_NAME_OBTENERDATOSGENERALES = "obtenerDatosGenerales";
	private static final String METHOD_NAME_HACERAPUESTA = "hacerApuesta";
	private static final String METHOD_NAME_OBTENERRANKING = "obtenerRanking";
	private static final String METHOD_NAME_OBTENERRANKINGEFECTIVIDAD = "obtenerRankingEfectividad";
	private static final String METHOD_NAME_OBTENERAPUESTASUSUARIO = "obtenerApuestasUsuario";
	private static final String METHOD_NAME_ELIMINARAPUESTA = "eliminarApuesta";
	private static final String METHOD_NAME_CREARNUEVOUSUARIO = "crearNuevoUsuario";
	private static final String METHOD_NAME_RECORDARLOGINUSUARIO = "recordarLoginUsuario";
	private static final String METHOD_NAME_OBTENERTEMPORADAS = "obtenerTemporadas";
	private static final String METHOD_NAME_OBTENERJORNADAS = "obtenerJornadas";
	//private static final String METHOD_NAME_DAMECOMENTARIOSFORO = "dameComentarioForo";
	//private static final String METHOD_NAME_INSERTARCOMENTARIOFORO = "insertarComentarioForo";
	private static final String METHOD_NAME_OBTENERPOSTSFORO = "obtenerPostsForo";
	private static final String METHOD_NAME_INSERTARPOSTFORO = "insertarPostForo";
	private static final String METHOD_NAME_OBTENERCOMENTARIOSFORO = "obtenerComentariosForo";
	private static final String METHOD_NAME_INSERTARCOMENTARIOFORO = "insertarComentarioForo";
	
	
	
	

	public int[] obtenerTemporadas(){
		try{
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_OBTENERTEMPORADAS);
			
			
			SoapSerializationEnvelope envelope = 
				new SoapSerializationEnvelope(SoapEnvelope.VER11); 
	
			envelope.setOutputSoapObject(request);
			HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
			
			try {
				androidHttpTransport.call(SOAP_ACTION, envelope);
				SoapObject resSoap = (SoapObject) envelope.bodyIn;
				
				int[] listaTemporadas = new int[resSoap.getPropertyCount()];
				
				for (int i = 0; i < listaTemporadas.length; i++)
				{
				       //SoapObject iq = (SoapObject)resSoap.getProperty(i);
				 
				       listaTemporadas[i] = Integer.parseInt(resSoap.getProperty(i).toString());
				       
				       
				}
				this.setTemporadas(listaTemporadas);
				return listaTemporadas;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	public int[] obtenerJornadas(){
		try{
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_OBTENERJORNADAS);
			
			
			SoapSerializationEnvelope envelope = 
				new SoapSerializationEnvelope(SoapEnvelope.VER11); 
	
			envelope.setOutputSoapObject(request);
			HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
			
			try {
				androidHttpTransport.call(SOAP_ACTION, envelope);
				SoapObject resSoap = (SoapObject) envelope.bodyIn;
				
				int[] listaJornadas = new int[resSoap.getPropertyCount()];
				
				for (int i = 0; i < listaJornadas.length; i++)
				{
					listaJornadas[i] = Integer.parseInt(resSoap.getProperty(i).toString());
				}
				this.setJornadas(listaJornadas);
				return listaJornadas;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	//public boolean crearUsuario(String usuario, String password, String memory, String nombre, String apellido1, String apellido2, String email){
	public int crearUsuario(String usuario, String password, String memory, String nombre, String apellido1, String apellido2, String email){
		int salida = 0;
		try
		{
			Usuario usu = new Usuario(usuario,password,nombre, 0, 0, "", 1);
			Persona per = new Persona(usuario, nombre, apellido1, apellido2, email);
			
			PropertyInfo piUsu = new PropertyInfo();
			piUsu.name = "usuario";
			piUsu.type = usu.getClass();
			piUsu.namespace = NAMESPACE; 
			
			PropertyInfo piPer = new PropertyInfo();
			piPer.name = "persona";
			piPer.type = per.getClass();
			piPer.namespace = NAMESPACE; 

			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_CREARNUEVOUSUARIO);
    		
			request.addProperty(piUsu, usu);
			request.addProperty(piPer, per);
			
    		
			SoapSerializationEnvelope envelope = 
					new SoapSerializationEnvelope(SoapEnvelope.VER11); 
				
				envelope.dotNet = false;
				envelope.encodingStyle = SoapSerializationEnvelope.XSD;
				envelope.setOutputSoapObject(request);
				
				envelope.addMapping(NAMESPACE, "Usuario", usu.getClass());
				envelope.addMapping(NAMESPACE, "Persona", per.getClass());
				
				Marshal floatMarshal = new MarshalFloat();
			    floatMarshal.register(envelope);
				
				HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    		try {
				androidHttpTransport.call(SOAP_ACTION, envelope);
				SoapObject resSoap = (SoapObject) envelope.bodyIn;

				if (envelope.bodyIn == null) {
					salida = 2; //sin conexion
				} 
				else if (((String)((SoapPrimitive)((Object)resSoap.getProperty(0))).toString()).startsWith("true"))
				{
					salida = 0;
				}else
				{
					salida = 1;
				}
			
     		//	return ((String)((SoapPrimitive)((Object)resSoap.getProperty(0))).toString()).startsWith("true");
    			
				return salida;
    		}
    		catch (Exception e) {

    			return 2;
/*    			if (envelope.bodyIn == null)
    				Toast.makeText(QuiniGoldApp.this, "Error al crear el usuario", Toast.LENGTH_LONG).show();
    			e.printStackTrace();
    			return false;
*/    			
    		}
			
			

    		
		}catch (Exception e)
		{
			e.printStackTrace();
			return 2;
			
		}
	}	
	public int validarUsuario(String usuario, String password) throws IOException, XmlPullParserException{
		inicializa();
		int salida = 0;
		int timeOut = 15000;

    		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_LOGIN);
    		
    		request.addProperty("usuario", usuario);
    		request.addProperty("password", password);
    		
    		SoapSerializationEnvelope envelope = 
    			new SoapSerializationEnvelope(SoapEnvelope.VER11); 

    		envelope.setOutputSoapObject(request);
    		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    

			androidHttpTransport.call(SOAP_ACTION, envelope);
				
			if (envelope.bodyIn == null) {
				salida = 2; //sin conexion
			} else {
				SoapObject resSoap = (SoapObject) envelope.getResponse();

				if (resSoap == null){
					salida = 1; //usuario o pwd incorrecto
				}else{
					Usuario usu = new Usuario();
					usu.setActivo(Integer.parseInt(resSoap.getProperty(0).toString()));
					usu.setApuAuto(Integer.parseInt(resSoap.getProperty(1).toString()));
					if (resSoap.getProperty(2).toString().startsWith("anyType"))
						usu.setApuFija("");
					else
						usu.setApuFija(resSoap.getProperty(2).toString());
					usu.setMemory(resSoap.getProperty(3).toString());
					usu.setPassword(resSoap.getProperty(4).toString());
					usu.setSaldo(Float.parseFloat(resSoap.getProperty(5).toString()));
					usu.setUsusario(resSoap.getProperty(6).toString());
					this.setUsuario(usu);
					salida = 0; //validacion ok
					
				}
			}
			
			return salida;

	}
	
	public Persona obtenerPersona(String usuario){
		try{
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_OBTENERPERSONA);
			
			request.addProperty("usuario", usuario);
			
			SoapSerializationEnvelope envelope = 
				new SoapSerializationEnvelope(SoapEnvelope.VER11); 
	
			envelope.setOutputSoapObject(request);
			HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
	
			try {
				androidHttpTransport.call(SOAP_ACTION, envelope);
				SoapObject resSoap =(SoapObject)envelope.getResponse();

				Persona per = new Persona(resSoap.getProperty(4).toString(), resSoap.getProperty(3).toString(), resSoap.getProperty(0).toString(), resSoap.getProperty(1).toString(), resSoap.getProperty(2).toString());
				
				this.setPersona(per);
				return per;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	
	public DatosGenerales obtenerDatosGenerales(){
		try{
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_OBTENERDATOSGENERALES);
			
		
			SoapSerializationEnvelope envelope = 
				new SoapSerializationEnvelope(SoapEnvelope.VER11); 
	
			envelope.setOutputSoapObject(request);
			HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
	
			try {
				androidHttpTransport.call(SOAP_ACTION, envelope);
				SoapObject resSoap =(SoapObject)envelope.getResponse();

				DatosGenerales dg = new DatosGenerales();
				dg.setTemporada(Integer.parseInt(resSoap.getProperty(3).toString()));
				dg.setJornada(Integer.parseInt(resSoap.getProperty(2).toString()));
				dg.setActiva(resSoap.getProperty(0).toString().equals("true"));
				dg.setDobles(Integer.parseInt(resSoap.getProperty(1).toString()));
				dg.setTriples(Integer.parseInt(resSoap.getProperty(4).toString()));
	
				
				this.setDatosGenerales(dg);
				return dg;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	
	public Quiniela[] obtenerQuiniela(DatosGenerales dg){
		DatosGenerales datGen = null;
		
		try{
			if (dg==null)
			datGen = this.getDatosGenerales();
			else
				datGen = dg;
				
			
			PropertyInfo pi = new PropertyInfo();
			pi.name = "datosGen";
			pi.type = datGen.getClass();
            pi.namespace = NAMESPACE; 
			 
           
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_OBTENERQUINIELA);
			
			request.addProperty(pi, datGen);

			SoapSerializationEnvelope envelope = 
				new SoapSerializationEnvelope(SoapEnvelope.VER11); 
			
			envelope.dotNet = false;
			envelope.encodingStyle = SoapSerializationEnvelope.XSD;
			envelope.setOutputSoapObject(request);
			
			envelope.addMapping(NAMESPACE, "DatosGenerales", datGen.getClass());
			
			HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

			try {
				androidHttpTransport.call(SOAP_ACTION, envelope);
				//SoapObject resSoap =(SoapObject)envelope.getResponse();
				SoapObject resSoap = (SoapObject) envelope.bodyIn;
				
				Quiniela[] listaQuiniela = new Quiniela[resSoap.getPropertyCount()];
				
				for (int i = 0; i < listaQuiniela.length; i++)
				{
				       SoapObject iq = (SoapObject)resSoap.getProperty(i);
				 
				       Quiniela qui = new Quiniela(Integer.parseInt(iq.getProperty(7).toString()), Integer.parseInt(iq.getProperty(2).toString()), i+1, iq.getProperty(0).toString(), iq.getProperty(1).toString(), Integer.parseInt(iq.getProperty(6).toString()), iq.getProperty(4).toString(), iq.getProperty(5).toString());
				       
				       listaQuiniela[i] = qui;
				       
				       
				}
				this.setQuiniela(listaQuiniela);
				return listaQuiniela;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	
	public boolean hacerApuesta (DatosGenerales dg, Usuario u, String apuesta){
		try{
			//DatosGenerales datGen = this.getDatosGenerales();
			
			PropertyInfo pi = new PropertyInfo();
			pi.name = "datosGen";
			pi.type = dg.getClass();
            pi.namespace = NAMESPACE; 
            
			PropertyInfo piu = new PropertyInfo();
			piu.name = "usuario";
			piu.type = u.getClass();
			piu.namespace = NAMESPACE; 
            
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_HACERAPUESTA);
			
			request.addProperty(pi, dg);
			request.addProperty(piu, u);
			request.addProperty("apuesta", apuesta);

			SoapSerializationEnvelope envelope = 
				new SoapSerializationEnvelope(SoapEnvelope.VER11); 
			
			envelope.dotNet = false;
			envelope.encodingStyle = SoapSerializationEnvelope.XSD;
			envelope.setOutputSoapObject(request);
			
			envelope.addMapping(NAMESPACE, "DatosGenerales", dg.getClass());
			envelope.addMapping(NAMESPACE, "Usuario", u.getClass());
			
			Marshal floatMarshal = new MarshalFloat();
		    floatMarshal.register(envelope);
		    
			HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

			try {
				androidHttpTransport.call(SOAP_ACTION, envelope);
				SoapObject resSoap = (SoapObject) envelope.bodyIn;
     			return ((String)((SoapPrimitive)((Object)resSoap.getProperty(0))).toString()).startsWith("true");
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
		}catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}
		
	}
	public DatosRanking[] obtenerRanking(DatosGenerales dg){
		try{
			//DatosGenerales datGen = this.getDatosGenerales();
			
			PropertyInfo pi = new PropertyInfo();
			pi.name = "datosGen";
			pi.type = dg.getClass();
            pi.namespace = NAMESPACE; 
			 
           
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_OBTENERRANKING);
			
			request.addProperty(pi, dg);

			SoapSerializationEnvelope envelope = 
				new SoapSerializationEnvelope(SoapEnvelope.VER11); 
			
			envelope.dotNet = false;
			envelope.encodingStyle = SoapSerializationEnvelope.XSD;
			envelope.setOutputSoapObject(request);
			
			envelope.addMapping(NAMESPACE, "DatosGenerales", dg.getClass());
			
			HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

			try {
				androidHttpTransport.call(SOAP_ACTION, envelope);
				//SoapObject resSoap =(SoapObject)envelope.getResponse();
				SoapObject resSoap = (SoapObject) envelope.bodyIn;
				
				DatosRanking[] listaRanking = new DatosRanking[resSoap.getPropertyCount()];
				
				for (int i = 0; i < listaRanking.length; i++)
				{
				       SoapObject iq = (SoapObject)resSoap.getProperty(i);
				 
				       DatosRanking qui = new DatosRanking(	Integer.parseInt(iq.getProperty(7).toString()), //temporada
				    		   								Integer.parseInt(iq.getProperty(5).toString()), //jornada
				    		   								iq.getProperty(9).toString(), 					//usuario
				    		   								Float.parseFloat(iq.getProperty(0).toString()), //aciertos 
				    		   								Integer.parseInt(iq.getProperty(8).toString()), //unos 
				    		   								Integer.parseInt(iq.getProperty(4).toString()), //equis 
				    		   								Integer.parseInt(iq.getProperty(3).toString()));//doses
				       
				       listaRanking[i] = qui;
				       
				       
				}
				this.setRankingPuntosGlobal(listaRanking);
				return listaRanking;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	
	public DatosRanking[] obtenerRankingEfectividad(DatosGenerales dg){
		try{
			//DatosGenerales datGen = this.getDatosGenerales();
			
			PropertyInfo pi = new PropertyInfo();
			pi.name = "datosGen";
			pi.type = dg.getClass();
            pi.namespace = NAMESPACE; 
			 
           
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_OBTENERRANKINGEFECTIVIDAD);
			
			request.addProperty(pi, dg);

			SoapSerializationEnvelope envelope = 
				new SoapSerializationEnvelope(SoapEnvelope.VER11); 
			
			envelope.dotNet = false;
			envelope.encodingStyle = SoapSerializationEnvelope.XSD;
			envelope.setOutputSoapObject(request);
			
			envelope.addMapping(NAMESPACE, "DatosGenerales", dg.getClass());
			
			HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

			try {
				androidHttpTransport.call(SOAP_ACTION, envelope);
				//SoapObject resSoap =(SoapObject)envelope.getResponse();
				SoapObject resSoap = (SoapObject) envelope.bodyIn;
				
				DatosRanking[] listaRanking = new DatosRanking[resSoap.getPropertyCount()];
				
				for (int i = 0; i < listaRanking.length; i++)
				{
				       SoapObject iq = (SoapObject)resSoap.getProperty(i);
				 
				       DatosRanking qui = new DatosRanking(	Integer.parseInt(iq.getProperty(7).toString()), //temporada
				    		   								Integer.parseInt(iq.getProperty(5).toString()), //jornada
				    		   								iq.getProperty(9).toString(), 					//usuario
				    		   								Float.parseFloat(iq.getProperty(0).toString()), //aciertos 
				    		   								Integer.parseInt(iq.getProperty(8).toString()), //unos 
				    		   								Integer.parseInt(iq.getProperty(4).toString()), //equis 
				    		   								Integer.parseInt(iq.getProperty(3).toString()));//doses
				       
				       listaRanking[i] = qui;
				       
				       
				}
				this.setRankingEfectividadGlobal(listaRanking);
				return listaRanking;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	public Apuesta[] obtenerApuestasUsuario(DatosGenerales dg, Usuario u){
		try{
			PropertyInfo pi = new PropertyInfo();
			pi.name = "datosGen";
			pi.type = dg.getClass();
            pi.namespace = NAMESPACE; 

            PropertyInfo piUsu = new PropertyInfo();
			piUsu.name = "usuario";
			piUsu.type = u.getClass();
            piUsu.namespace = NAMESPACE; 
           
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_OBTENERAPUESTASUSUARIO);
			
			request.addProperty(pi, dg);
			request.addProperty(piUsu, u);

			SoapSerializationEnvelope envelope = 
				new SoapSerializationEnvelope(SoapEnvelope.VER11); 
			
			envelope.dotNet = false;
			envelope.encodingStyle = SoapSerializationEnvelope.XSD;
			envelope.setOutputSoapObject(request);
			
			envelope.addMapping(NAMESPACE, "DatosGenerales", dg.getClass());
			envelope.addMapping(NAMESPACE, "Usuario", u.getClass());
			
			Marshal floatMarshal = new MarshalFloat();
		    floatMarshal.register(envelope);
	
		    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

			try {
				androidHttpTransport.call(SOAP_ACTION, envelope);
				//SoapObject resSoap =(SoapObject)envelope.getResponse();
				SoapObject resSoap = (SoapObject) envelope.bodyIn;
				
				Apuesta[] listaApuestas = new Apuesta[resSoap.getPropertyCount()];
				
				for (int i = 0; i < listaApuestas.length; i++)
				{
				       SoapObject iq = (SoapObject)resSoap.getProperty(i);
				 
				       Apuesta apuAux = new Apuesta(	Integer.parseInt(iq.getProperty(4).toString()), //id
				    		   						Integer.parseInt(iq.getProperty(6).toString()), //temporada
				    		   						Integer.parseInt(iq.getProperty(5).toString()), //jornada
				    		   								iq.getProperty(8).toString(), //usuario					
				    		   								iq.getProperty(1).toString(), //apuesta					
				    		   								Integer.parseInt(iq.getProperty(0).toString()),//aciertos  
				    		   								Integer.parseInt(iq.getProperty(2).toString()),//doses  
				    		   								Integer.parseInt(iq.getProperty(3).toString()),//equis  
				    		   								Integer.parseInt(iq.getProperty(7).toString()));//unos
				       listaApuestas[i] = apuAux;
				       
				       
				}
				this.setApuestasUsuario(listaApuestas);
				return listaApuestas;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	
	public boolean eliminarApuestasUsuario(DatosGenerales dg, Usuario u, String apuesta){
		return false;
	}
	
	public boolean recordarLoginUsuario(String email, String Memory){
		try
		{
    		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_RECORDARLOGINUSUARIO);
    		
    		request.addProperty("email", email);
    		request.addProperty("memory", Memory);
    		
    		SoapSerializationEnvelope envelope = 
    			new SoapSerializationEnvelope(SoapEnvelope.VER11); 

    		envelope.setOutputSoapObject(request);
    		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    		try {
				androidHttpTransport.call(SOAP_ACTION, envelope);
				SoapObject resSoap = (SoapObject) envelope.bodyIn;
				//String resultado = ((SoapPrimitive)((Object)resSoap.getProperty(0))).toString();
				//boolean rdo = resultado.startsWith("true");
				
     			return ((String)((SoapPrimitive)((Object)resSoap.getProperty(0))).toString()).startsWith("true");
    		}
    		catch (Exception e) {

    			if (envelope.bodyIn == null)
    				Toast.makeText(QuiniGoldApp.this, "Sin conexion", Toast.LENGTH_LONG);
    			e.printStackTrace();
    			return false;
    		}
			
			

    		
		}catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean insertarPostForo (DatosPostForoSimple dpf){
		try
		{
			//<element name="datosPF" type="tns1:DatosPostForo"/>
			PropertyInfo pi = new PropertyInfo();
			pi.name = "datosPFS";
			pi.type = dpf.getClass();
            pi.namespace = NAMESPACE; 
            
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_INSERTARPOSTFORO);
			
			request.addProperty(pi, dpf);

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
			
			envelope.dotNet = false;
			envelope.encodingStyle = SoapSerializationEnvelope.XSD;
			envelope.setOutputSoapObject(request);
			
			envelope.addMapping(NAMESPACE, "DatosPostForoSimple", dpf.getClass());
			
			HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

			try {
				androidHttpTransport.call(SOAP_ACTION, envelope);
				SoapObject resSoap = (SoapObject) envelope.bodyIn;
     			return ((String)((SoapPrimitive)((Object)resSoap.getProperty(0))).toString()).startsWith("true");
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
		}catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}
		
	}
	public boolean insertarRespuestaPostForo (DatosComentarioForo dcf){
		try
		{
			PropertyInfo pi = new PropertyInfo();
			pi.name = "datosCF";
			pi.type = dcf.getClass();
            pi.namespace = NAMESPACE; 
            
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_INSERTARCOMENTARIOFORO);
			
			request.addProperty(pi, dcf);

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
			
			envelope.dotNet = false;
			envelope.encodingStyle = SoapSerializationEnvelope.XSD;
			envelope.setOutputSoapObject(request);
			
			envelope.addMapping(NAMESPACE, "DatosComentarioForo", dcf.getClass());
			
			HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

			try {
				androidHttpTransport.call(SOAP_ACTION, envelope);
				SoapObject resSoap = (SoapObject) envelope.bodyIn;
     			return ((String)((SoapPrimitive)((Object)resSoap.getProperty(0))).toString()).startsWith("true");
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
		}catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}
		
	}
	
	
	public DatosPostForo[] obtenerPostsForo(int paginacion, int cantidad)
	{
		try
		{
/*
			SoapSerializationEnvelope envelope = 
				new SoapSerializationEnvelope(SoapEnvelope.VER11); 
	
			envelope.setOutputSoapObject(request);
			HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
	
			try {
				androidHttpTransport.call(SOAP_ACTION, envelope);
 
*/			
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_OBTENERPOSTSFORO);
			
    		request.addProperty("paginacion", paginacion);
    		request.addProperty("cantidad", cantidad);

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
			
			envelope.dotNet = false;
			envelope.encodingStyle = SoapSerializationEnvelope.XSD;
			envelope.setOutputSoapObject(request);
			
			HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

			try
			{
				androidHttpTransport.call(SOAP_ACTION, envelope);
				SoapObject resSoap = (SoapObject) envelope.bodyIn;
				
				DatosPostForo[] listaForo = new DatosPostForo[resSoap.getPropertyCount()];
				
				for (int i = 0; i < listaForo.length; i++)
				{
				       SoapObject iq = (SoapObject)resSoap.getProperty(i);
				 
				       DatosPostForo foro = new DatosPostForo (	Integer.parseInt(iq.getProperty(2).toString()), 	//id
				    		   									iq.getProperty(7).toString(), 						//usuario
				    		   									iq.getProperty(5).toString(), 						//titulo
				    		   									iq.getProperty(3).toString(), 						//mensaje 
				    		   									iq.getProperty(1).toString(), 						//fecha 
				    		   									Integer.parseInt(iq.getProperty(4).toString()), 	//respuestas 
				    		   									iq.getProperty(6).toString() );						//ult_respuesta
				       
				       SoapObject comentarios = (SoapObject)iq.getProperty(0);
				       DatosComentarioForo[] listaComentario = new DatosComentarioForo[comentarios.getPropertyCount()];
				       for (int j = 0; j < listaComentario.length; j++)
				       {
				    	   SoapObject com = (SoapObject)comentarios.getProperty(j);
				    	   
				    	   DatosComentarioForo coment = new DatosComentarioForo(Integer.parseInt(com.getProperty(2).toString()), //id
				    			   												com.getProperty(4).toString(),//usuario
				    			   												Integer.parseInt(com.getProperty(3).toString()),//idpost
				    			   												com.getProperty(0).toString(),//comentario
				    			   												com.getProperty(1).toString());//fecha
					       listaComentario[j] = coment;
				       }
				       
				       foro.setCometarios(listaComentario);
				       listaForo[i] = foro;
				}
				
				this.setQgForo(listaForo);
				
				return listaForo;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}
	public DatosComentarioForo[] obtenerComentariosForo(int idPost, int paginacion, int cantidad)
	{
		try
		{
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_OBTENERCOMENTARIOSFORO);
			
			request.addProperty("idpost", idPost);
    		request.addProperty("paginacion", paginacion);
    		request.addProperty("cantidad", cantidad);

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
			
			envelope.dotNet = false;
			envelope.encodingStyle = SoapSerializationEnvelope.XSD;
			envelope.setOutputSoapObject(request);
			
			HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

			try
			{
				androidHttpTransport.call(SOAP_ACTION, envelope);
				SoapObject resSoap = (SoapObject) envelope.bodyIn;
				
				DatosComentarioForo[] listaForo = new DatosComentarioForo[resSoap.getPropertyCount()];
				
				for (int i = 0; i < listaForo.length; i++)
				{
				       SoapObject iq = (SoapObject)resSoap.getProperty(i);
				 
				       DatosComentarioForo foro = new DatosComentarioForo (	Integer.parseInt(iq.getProperty(2).toString()), 	//id
				    		   									iq.getProperty(4).toString(), 									//usuario
				    		   									Integer.parseInt(iq.getProperty(3).toString()), 				//idpost
				    		   									iq.getProperty(0).toString(), 									//comentario 
				    		   									iq.getProperty(1).toString() 									//fecha 
				    		   									 
				    		   									);

				       listaForo[i] = foro;
				}
				
				this.setQgForo(listaForo);
				
				return listaForo;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

}
